//
//  SCCountryViewController.m
//  LastOne
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "SCCountryViewController.h"
#import "SCShopInfoCollectionViewCell.h"
#import "SCImageCollectionViewCell.h"
#import "SCButtonTraverCollectionReusableView.h"
#import "SCTitleLabelCollectionReusableView.h"
#import "HTTPMethod.h"
#import <UIImageView+WebCache.h>
#import "SCCityViewController.h"
#import "SCWebViewController.h"
#import "SCPageScrollView.h"
#import "SCAllCountryViewController.h"
#import "NightHeader.h"
#import "NightHeader.h"
#import "GiFHUD.h"
#import "FitHeader.h"
#import "NSString+EmString.h"
//#import "NSString+EmString.h"
@interface SCCountryViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)NSMutableDictionary *dataDic;
@property(nonatomic, retain)NSMutableArray *dataSource;
@property(nonatomic, strong)SCPageScrollView *pageView;
@end

@implementation SCCountryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //加载页面
    [GiFHUD setGifWithImageName:@"loading.gif"];
    [self getData];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.sectionInset = UIEdgeInsetsMake(5 * FitHeight, 5 * FitWidth, 5 * FitHeight, 5 * FitWidth);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView NightWithType:UIViewColorTypeNormal];
    [_collectionView NightWithType:UIViewColorType2];
    [_collectionView registerClass:[SCImageCollectionViewCell class] forCellWithReuseIdentifier:@"imageCell"];
    [_collectionView registerClass:[SCShopInfoCollectionViewCell class] forCellWithReuseIdentifier:@"shopCell"];
    
    //注册分区头视图(button点击)
    [_collectionView registerClass:[SCButtonTraverCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"buttonHeader"];
    //注册分区头视图
    [_collectionView registerClass:[SCTitleLabelCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"titleHeader"];
    
    [self.view addSubview:_collectionView];
}
//获取数据
- (void)getData;
{
     [GiFHUD show];
    self.dataDic = [NSMutableDictionary dictionary];
    [HTTPMethod getDataByString:[NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/country_detail?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=861138023945773&track_app_version=6.7&track_app_channel=360m&track_device_info=mx3&track_os=Android4.2.1&app_installtime=1443017336409&lat=48.856613&lon=2.352222&country_id=%@", _countryId] BodyDic:nil WithDataBlock:^(id data) {
        [self.dataDic setValuesForKeysWithDictionary:[data objectForKey:@"data"]];
        [_collectionView reloadData];
        [GiFHUD dismiss];
    }];
}
//返回分区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (_dataDic.count == 0) {
        return 0;
    }
    return 2;
}
//返回每个分区的items数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_dataDic.count == 0) {
        return 0;
    }
    if (section == 0) {
        return [[_dataDic objectForKey:@"hot_city"] count];
    }
    else{
        return [[_dataDic objectForKey:@"new_discount"] count];
    }
}
//
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //热门城市的图片cell
    if (indexPath.section == 0) {
        SCImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imageCell" forIndexPath:indexPath];
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[[[_dataDic objectForKey:@"hot_city"] objectAtIndex:indexPath.row] objectForKey:@"photo"]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        cell.nameString =  [[[_dataDic objectForKey:@"hot_city"] objectAtIndex:indexPath.row] objectForKey:@"cnname"];
        cell.ennameString = [[[_dataDic objectForKey:@"hot_city"] objectAtIndex:indexPath.row] objectForKey:@"enname"];
        [cell NightWithType:UIViewColorTypeNormal];
        return cell;
    }
    
    //超值自由行的cell
    else{
        SCShopInfoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"shopCell" forIndexPath:indexPath];
        cell.kind = 0;
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[[[_dataDic objectForKey:@"new_discount"] objectAtIndex:indexPath.row] objectForKey:@"photo"]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        cell.nameLabel.text = [[[_dataDic objectForKey:@"new_discount"] objectAtIndex:indexPath.row] objectForKey:@"title"];
        
       cell.priceLabel.text = [NSString stringWithFormat:@"%@元起", [NSString getStringByEmString:[[[_dataDic objectForKey:@"new_discount"] objectAtIndex:indexPath.row] objectForKey:@"price"]]];
        cell.priceOffLabel.text = [[[_dataDic objectForKey:@"new_discount"] objectAtIndex:indexPath.row] objectForKey:@"priceoff"];
        
        [cell NightWithType:UIViewColorTypeNormal];
        return cell;
    }
}

//每个item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake((self.view.frame.size.width - 25 * FitWidth) / 2, 120 * FitHeight);
    }
    else{
        return CGSizeMake(self.view.frame.size.width - 10 * FitWidth, 100 * FitHeight);
    }
}

//分区的头视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        //找到buttonHeader的header
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"buttonHeader" forIndexPath:indexPath];
        
       //添加轮播图
        _pageView = [[SCPageScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * FitHeight) PicArray:[_dataDic objectForKey:@"photos"]];
        [header addSubview:_pageView];
        
        //详情
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(_pageView.frame.origin.x, _pageView.frame.origin.y + _pageView.frame.size.height, self.view.frame.size.width, 50 * FitHeight)];
        label.numberOfLines = 2;
        [header addSubview:label];
        //label.textColor = [UIColor grayColor];
        label.font = [UIFont systemFontOfSize:15 * FitWidth];
        label.text = [_dataDic objectForKey:@"entryCont"];
        
        label.textType = [NSNumber numberWithInteger:LabelColorGray] ;
        
        [label NightWithType:UIViewColorTypeNormal];
        //添加titleLable 热门城市
        UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(label.frame.origin.x + 10 * FitWidth, label.frame.origin.y + label.frame.size.height, label.frame.size.width, 50 * FitHeight)];
        [titleLable NightWithType:UIViewColorTypeNormal];
        titleLable.font = [UIFont systemFontOfSize:17 * FitWidth];
        titleLable.text = @"热门城市";
        [header addSubview:titleLable];
     
        //添加查看全部
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"查看全部" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor colorWithRed:0.32 green:0.79 blue:0.64 alpha:1] forState:UIControlStateNormal];
        button.titleLabel.textAlignment = 2;
        button.titleLabel.font = [UIFont systemFontOfSize:15 * FitWidth];
        button.frame = CGRectMake(230 * FitWidth, titleLable.frame.origin.y, 190 * FitWidth, 50 * FitHeight);
        [header addSubview:button];
        
        return header;
    }
    //超值自由行
    else{
        SCTitleLabelCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"titleHeader" forIndexPath:indexPath];
        header.label.text = @"超值自由行";
        [header.label NightWithType:UIViewColorTypeNormal];
        return header;
    }
}

//点击全部跳转更多的方法
-(void)buttonAction
{
    SCAllCountryViewController *vc = [[SCAllCountryViewController alloc] init];
    vc.cityId = _countryId;
    [self.navigationController pushViewController:vc animated:YES];
}

//分区的视图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeMake(self.view.frame.size.width, 300 * FitHeight );
    }
    else{
        return CGSizeMake(self.view.frame.size.width - 10 * FitWidth, 40 * FitHeight);
    }
}

//item的点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //热门城市的跳转方法
    if (indexPath.section == 0) {
        SCCityViewController *cityVC = [[SCCityViewController alloc] init];
        cityVC.cityId = [[[_dataDic objectForKey:@"hot_city"] objectAtIndex:indexPath.row] objectForKey:@"id"];
        [self.navigationController pushViewController:cityVC animated:YES];
    }
    //超值自由行跳转网页
    else{
        SCWebViewController *webVC = [[SCWebViewController alloc] init];
        webVC.urlString = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_android&track_app_version=6.7&track_deviceid=861138023945773", [[[_dataDic objectForKey:@"new_discount"] objectAtIndex:indexPath.row] objectForKey:@"id"]];
        [self.navigationController pushViewController:webVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
